/*
 * Log4JAdapter.java  0.4.0 / Oct 27, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.logging.adapters;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

import com.cute4j.Cute4j;
import com.cute4j.logging.LoggingAdapter;

/**
 * Logging Adapter for Log4J Logging API.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class Log4JAdapter implements LoggingAdapter { // NOPMD

// Public Static Fields. -------------------------------------------------------

  /**
   * Default conversion pattern.
   */
  public static final String DEFAULT_PATTERN;

// Private Static Fields. ------------------------------------------------------

  private static final String FQCN;
  private static final String NATIVE_FQCN;

//Static Initialization. ------------------------------------------------------

  static {
    DEFAULT_PATTERN = "[%d{yyyy-MM-dd HH:mm:ss}][%p][%l] %m%n";
    FQCN = com.cute4j.logging.Logger.class.getName();
    NATIVE_FQCN = Log4JAdapter.class.getName();
    if (!Logger.getRootLogger().getAllAppenders().hasMoreElements()) {
      Logger logger = Logger.getLogger(Cute4j.class.getName());
      if (!logger.getAllAppenders().hasMoreElements()) {
        PatternLayout patternLayout = new PatternLayout(DEFAULT_PATTERN);
        logger.addAppender(new ConsoleAppender(patternLayout,
                                               ConsoleAppender.SYSTEM_ERR));
      }
    }
  }

// Private Fields. -------------------------------------------------------------

  private final Logger logger; // NOPMD - GC

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new Log4JAdapter object.
   */
  public Log4JAdapter() {
    logger = Logger.getLogger(Cute4j.class.getName()); // NOPMD
  }

// LoggingAdapter Interface Implementation. ------------------------------------

  /**
   * {@inheritDoc}
   */
  public String getName() {
    return "Log4J Logging Adapter";
  }

  /**
   * {@inheritDoc}
   */
  public boolean isLoggable(int level) {
    return logger.isEnabledFor(convertLevel(level));
  }

  /**
   * {@inheritDoc}
   */
  public int getLevel() {
    return convertLevel(logger.getLevel());
  }

  /**
   * {@inheritDoc}
   */
  public void setLevel(int level) {
    logger.setLevel(convertLevel(level));
  }

  /**
   * {@inheritDoc}
   */
  public void log(int level, String message, Throwable t) {
    logger.log(FQCN, convertLevel(level), message, t);
  }

  /**
   * {@inheritDoc}
   */
  public void log(int level, String fileName, int lineNumber, String message,
                  Throwable t) {
    String nativeMessage = "[" + fileName + ":" + lineNumber + "] " + message;
    logger.log(NATIVE_FQCN, convertLevel(level), nativeMessage, t);
  }

// Private Methods. ------------------------------------------------------------

  private Level convertLevel(int level) {
    switch (level) {
      case com.cute4j.logging.Logger.LEVEL_INFO:
        return Level.INFO;
      case com.cute4j.logging.Logger.LEVEL_WARN:
        return Level.WARN;
      default:
        return Level.ERROR;
    }
  }

  private int convertLevel(Level level) {
    int l = level.toInt();
    if (l <= Level.INFO.toInt()) {
      return com.cute4j.logging.Logger.LEVEL_INFO;
    } else if (l <= Level.WARN.toInt()) {
      return com.cute4j.logging.Logger.LEVEL_WARN;
    }
    return com.cute4j.logging.Logger.LEVEL_ERROR;
  }

}
